package com.splant.android.smart.SystemDefinedLevel.FilesManager;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by JimOPC on 15-3-24.
 */
public class FilesManager {


    private static final String SYSTEM_DIRECTORYPATH_NAME="Nothing_Com";
    private Context curContext;
    private FileHelper fileHelper;
    public static String getSDcardPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        } else {
            return null;
        }
        return sdDir.toString();
    }
    public static String getSystemDirectoryPath()
    {
        String sdCardPath=getSDcardPath();
        if (sdCardPath==null)
            throw new NullPointerException("未连接SD卡");

        return sdCardPath+"/"+SYSTEM_DIRECTORYPATH_NAME+"/";
    }
    public FilesManager(Context context) {
        curContext = context;
        fileHelper = new FileHelper(curContext);
    }

    public int saveFile(String filePath, String fileName, String fileSuffixName, String fileContext, String charSetName) {
        String fileSrc = filePath + "\\" + fileName + fileSuffixName;
        return saveFile(fileSrc, fileContext, charSetName);
    }

    public int saveFile(String fileSrc, String fileContext, String charSetName) {
        byte[] datas = null;
        try {
            datas = fileContext.getBytes(charSetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return saveFile(fileSrc, datas);
    }

    public int saveFile(String fileSrc, byte[] fileContext) {
        int result = FileErrorType.SUCCESS;
        try
        {
            File file = new File(fileSrc);
            if (!file.exists() || file.isDirectory())
                file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(fileContext);
            fos.close();

        } catch (IOException e) {
            result = FileErrorType.Err_SAVEFAIL;
        }
        return result;
    }

    public boolean deleteFile(String fileSrc) {
        return fileHelper.delFile(fileSrc);
    }

    public String openFile(String fileSrc, String charSetName) {
        String relStr = "";
        try
        {
            File file = new File(fileSrc);
            FileInputStream fis = new FileInputStream(file);
            int length = fis.available();
            byte[] buffer = new byte[length];
            fis.read(buffer);
            relStr = new String(buffer, charSetName);
            fis.close(); //关闭输入流
        }
        catch (IOException e) {

        }
        return relStr;
    }

    public static byte[] readFile(String fileSrc) {
        byte[] buffer = null;
        try
        {
            File file = new File(fileSrc);
            FileInputStream fis = new FileInputStream(file);
            int length = fis.available();
            buffer = new byte[length];
            fis.read(buffer);
            fis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    public int appendFileContext(String filePath, String fileName, String fileSuffixName, String appendFileContext) {
        String fileSrc = filePath + "\\" + fileName + "\\" + fileSuffixName;
        return appendFileContext(fileSrc, appendFileContext);
    }

    public int appendFileContext(String fileSrc, String appendFileContext) {
        int relValue = FileErrorType.SUCCESS;
        try {
            FileWriter fileWriter = new FileWriter(fileSrc, true);
            fileWriter.write(appendFileContext);
            fileWriter.close();
        } catch (IOException e) {
            relValue = FileErrorType.Err_APPENDFAIL;
        }
        return relValue;
    }

}
